package com.xckj.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.adrninistrator.jacg.common.enums.ConfigDbKeyEnum;
import com.adrninistrator.jacg.common.enums.OtherConfigFileUseSetEnum;
import com.adrninistrator.jacg.conf.ConfigureWrapper;
import com.adrninistrator.jacg.util.JACGFileUtil;
import com.adrninistrator.jacg.util.JACGUtil;
import com.xckj.dao.JacgAllowedClassPrefixMapper;
import com.xckj.entity.AllowedClassPre;
import com.xckj.service.AllowedClassPreService;
import com.xckj.utils.ReturnT;
import com.xckj.utils.WriteConfigFile;

/**
 * @description 数据库配置
 * @author yanchf
 * @date 2023-12-09
 */
@Service
public class AllowedClassPreServiceImpl implements AllowedClassPreService {

	@Value("${app.project.name}")
	public String appName;
	
	@Override
	public Object insert(AllowedClassPre allowedClassPre) {
		// valid
		if (allowedClassPre == null) {
			return ReturnT.error("必要参数缺失");
        }
    	String confFile = OtherConfigFileUseSetEnum.OCFUSE_ALLOWED_CLASS_PREFIX.getKey();
//		boolean isSucc = JACGFileUtil.writeToFile(confFile, allowedClassPre.getClassNamePrefix());
    	boolean isSucc = true;
    	return ReturnT.success(isSucc);
	}


	@Override
	public Object delete(int id) {
		boolean isSucc = false;
        return ReturnT.success(isSucc);
	}


	@Override
	public Object update(AllowedClassPre allowedClassPre) {
		return insert(allowedClassPre);
	}


	@Override
	public AllowedClassPre load(String id) {
    	String confFile = OtherConfigFileUseSetEnum.OCFUSE_ALLOWED_CLASS_PREFIX.getKey();
    	Set<String> configSet = JACGFileUtil.readFile2Set(JACGUtil.getInputRootPath() + confFile);
    	AllowedClassPre allowedClassPre = null;
    	for(String conf : configSet) {
			allowedClassPre = new AllowedClassPre();
			allowedClassPre.setClassNamePrefix(conf);
			allowedClassPre.setAppName(appName);
			break;
		}
		return allowedClassPre;
	}


	@Override
	public Map<String,Object> pageList(int offset, int pagesize) {
    	String confFile = OtherConfigFileUseSetEnum.OCFUSE_ALLOWED_CLASS_PREFIX.getKey();
    	Set<String> configSet = JACGFileUtil.readFile2Set(JACGUtil.getInputRootPath() + confFile);
		List<AllowedClassPre> list = new ArrayList<AllowedClassPre>();
    	for(String conf : configSet) {
			AllowedClassPre allowedClassPre = new AllowedClassPre();
			allowedClassPre.setClassNamePrefix(conf);
			allowedClassPre.setAppName(appName);
			list.add(allowedClassPre);
		}
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", list);
		result.put("totalCount", list.size());
		return ReturnT.success(result);
	}

}